package Io;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2023-04-05
 * Time: 22:35
 */
public class test12 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的路径");
        String path = sc.next();
        File file = new File(path);
        if(!file.isDirectory()){
            System.out.println("输入非文件目录");
            return;
        }
        System.out.println("请输入要查找文件的字符");
        String findStr= sc.next();
        chaoZhao(file,findStr);
    }

    private static void chaoZhao(File file, String findStr) throws IOException {
       File[] files =file.listFiles();
       if(files==null || files.length==0 ){
           return;
       }
        for (File fi: files) {
            if(fi.isDirectory()){
chaoZhao(fi,findStr);
        }else if(fi.isFile()){
String con = readFile(fi);
if(findStr.contains(con)){
    System.out.println("在"+fi.getAbsolutePath()+"找到关键词"+findStr);
}
            }
            else {
                continue;
            }
            
        }
    }

    private static String readFile(File fi) throws IOException {
        StringBuffer stringBuffer = new StringBuffer();
        try (Reader reader = new FileReader(fi)){
while (true){
    int i = reader.read();
    if(i==-1){
        break;
    }
    stringBuffer.append((char) i);
}
        }
        return stringBuffer.toString();
    }
}
